package com.lizhi.updata;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lizhi.model.BookModel;
import com.lizhi.util.Util;

public class ChangArticle {
	private String url = "https://freebook.firebaseio.com/articles.json";

	public ChangArticle() {
		
		try {
			List<BookModel> bookModels = new ArrayList<BookModel>();
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet httpPost = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(httpPost);
			InputStream inputStream = response.getEntity().getContent();
			JSONObject jsonObject = JSON.parseObject(Util.readStream(inputStream));

			Set<String> keySet = jsonObject.keySet();
			for (String string : keySet) {
				BookModel bookModel=JSON.toJavaObject(jsonObject.getJSONObject(string),BookModel.class);
				bookModels.add(bookModel);
				System.out.println(bookModel.name);
			}
			
			System.out.println("pageNUM:"+bookModels.size());
			
			Util.clearData(url);

			Calendar calendar = Calendar.getInstance();
			calendar.set(2015, 3, 13);

			for (int i = 0; i < bookModels.size(); i++) {
				BookModel bookModel = bookModels.get(i);
				bookModel.time=String.valueOf(calendar.getTimeInMillis());
				
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
				System.out.println(format.format(Long.parseLong(bookModel.time)));
				
				calendar.add(Calendar.MILLISECOND, 1);
				if (i % 4 == 0) {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
				new UploadDataTask(url, JSON.toJSONString(bookModel)).start();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		new ChangArticle();

	}

}
